using System.Reflection;

namespace 反射案例
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 获取当前执行程序集的Assembly对象  
            Assembly assembly = Assembly.GetExecutingAssembly();

            textBox1.Text = assembly.GetName().Name;

            // 获取程序集中的所有类型（包括类、接口、枚举等）  
            Type[] types = assembly.GetTypes();

            // 遍历所有类型，并只打印类（不包括接口、枚举等）  
            foreach (Type type in types)
            {
                // IsClass属性用于判断类型是否为类  
                if (type.IsClass)
                {
                    //Console.WriteLine(type.FullName); // 打印类的全名  

                    comboBox1.Items.Add(type.FullName);
                }
            }
        }


        /// <summary>
        /// 选中类名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            // 类名（包括命名空间）  
            string className = comboBox1.SelectedItem as string;

            if(className.IsNotEmpty())
            {
                listBox1.Items.Clear();
                listBox2.Items.Clear();
                listBox3.Items.Clear();

                // 加载包含该类的程序集（这里假设类在当前执行程序集中）  
                Assembly assembly = Assembly.GetExecutingAssembly();

                // 根据类名获取Type对象  
                Type type = assembly.GetType(className);

                if (type != null)
                {
                    // 获取类的所有属性  
                    PropertyInfo[] properties = type.GetProperties();
                    foreach (PropertyInfo property in properties)
                    {
                        // Console.WriteLine("Property: " + property.Name);

                        listBox1.Items.Add("Property: " + property.Name);

                    }

                    // 获取类的所有字段（包括public, private, protected等）  
                    FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
                    foreach (FieldInfo field in fields)
                    {
                        // Console.WriteLine("Field: " + field.Name);

                        listBox2.Items.Add("Field: " + field.Name);
                    }

                    // 获取类的所有方法（包括public, private, protected等）  
                    MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
                    foreach (MethodInfo method in methods)
                    {
                       // Console.WriteLine("Method: " + method.Name);

                        listBox3.Items.Add("Method: " + method.Name);
                    }
                }
                else
                {
                    // Console.WriteLine("Type not found: " + className);

                    listBox1.Items.Add("Type not found: " + className);
                }


            }

            
        }
    }




    public class People
    {

        private string name;
        private int age;
        private string sex;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public int Age { get; set; }
        public string Sex { get; set; }


        public People() { }
        public People(string name, int age, string sex) 
        { 
        
            this.name = name;
            this.age = age;
            this.sex = sex;
        }


        public string Info()
        {
            return name+":"+age+":"+sex;
        }

    }


}
